﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Looptribe.WinRT.Toolkit;

namespace Looptribe.Moodlin8.ViewModel
{
    public class DayEventsViewModel : LoadingViewModel
    {
        private readonly ObservableCollection<EventWrapper> _events = new ObservableCollection<EventWrapper>();
        public ObservableCollection<EventWrapper> Events
        {
            get { return _events; }
        }

        private DateTime _date;
        public DateTime Date { get { return _date; } }

        public async void LoadContent(DateTime date)
        {
            _date = date.Date;
            await Refresh();
        }

        public async override Task Refresh()
        {
            IsLoading = true;
            Events.Clear();
            var events = (await Api.GetEventsAsync(DateTime.Now.AddMonths(-1), DateTime.Now.AddMonths(1))).Data;
            IsLoading = false;

            if (events != null)
            {
                Events.AddMany(events
                    .Where(a => a.TimeStart.Date == Date)
                    .OrderBy(a => a.TimeStart)
                    .Select(a => new EventWrapper(a)));
            }
        }
    }
}
